package edu.vt.ece3574.ninjatag;

/****************************************************************************
 * Copyright 2012 Landon Biggs, Chris Dusold, Brad Russell, and Craig Wertz	*
 * Licensed under the Apache License, Version 2.0 (the "License");			*
 * you may not use this file except in compliance with the License.			*
 * You may obtain a copy of the License at									*
 * 																			*
 * http://www.apache.org/licenses/LICENSE-2.0								*
 * 																			*
 * Unless required by applicable law or agreed to in writing, software		*
 * distributed under the License is distributed on an "AS IS" BASIS,		*
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.	*
 * See the License for the specific language governing permissions and		*
 * limitations under the License.											*
 ****************************************************************************/

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class gameStats_screen extends Activity implements OnClickListener {
	
	private Button map, menu;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamestats_screen);
        
        map = (Button)findViewById(R.id.mapButton);
        menu = (Button)findViewById(R.id.menuButton);
        
        map.setOnClickListener(this);
        menu.setOnClickListener(this);
	}
	
	/*
	 *	The next couple methods are used to override the menu and back physical buttons on the phone.
	 *	onCreateOptionsMenu sets the menu to the custom 'main_menu'
	 *	onOptionsItemSelected starts an activity based on the selected option in the menu
	 * 	onBackPressed sends the app to the previous screen.
	 * 
	 */
	
    @Override
	public void onBackPressed(){
		Intent newIntent = new Intent(gameStats_screen.this, map_screen.class);
		startActivity(newIntent);
		return;
	}
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.main_menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	switch(item.getItemId() ){
    	
    	case R.id.menu_home:
    		Intent mainScreenIntent = new Intent(gameStats_screen.this, main_screen.class);
    		startActivity(mainScreenIntent);
    		finish();
    		break;
    		
    	
    	case R.id.menu_map:
    		Intent mapScreenIntent = new Intent(gameStats_screen.this, map_screen.class);
    		startActivity(mapScreenIntent);
    		finish();
    		break;
    		
    	case R.id.menu_lock_or:
			
			if(getOrientation() == Configuration.ORIENTATION_PORTRAIT){
				this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
				break;
			}
		
			else if(getOrientation() == Configuration.ORIENTATION_LANDSCAPE){
				this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
				break;
			}
			else Toast.makeText(getBaseContext(), "Error: orientation was not locked", Toast.LENGTH_SHORT).show();
			break;
			
    	case R.id.menu_unlock_or:
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
			break;
    	}
    	return true;
    }

	@Override
	public void onClick(View v) {
		
		Intent mapScreenIntent = new Intent(gameStats_screen.this, map_screen.class);
		Intent mainScreenIntent = new Intent(gameStats_screen.this, main_screen.class);
	
		switch(v.getId()){
		
		case R.id.mapButton:
			startActivity(mapScreenIntent);
			finish();
			break;
		
		case R.id.menuButton:
			startActivity(mainScreenIntent);
			finish();
			break;
		}
	}
	
	public int getOrientation()
	{
	    Display getOrient = getWindowManager().getDefaultDisplay();
	    int orientation = Configuration.ORIENTATION_UNDEFINED;
	    if(getOrient.getWidth()==getOrient.getHeight()){
	        orientation = Configuration.ORIENTATION_SQUARE;
	    } else{ 
	        if(getOrient.getWidth() < getOrient.getHeight()){
	            orientation = Configuration.ORIENTATION_PORTRAIT;
	        }else { 
	             orientation = Configuration.ORIENTATION_LANDSCAPE;
	        }
	    }
	    return orientation;
	}
}
